package com.example.security_demo.sms;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 定义 短信验证码 验证主体
 * 在这里可以将 验证码也添加进来
 * @author zzh
 * @date 2022/4/23
 * @since 1.0
 */
public class SmsAuthenticationToken extends AbstractAuthenticationToken {

    /**
     *  principle： 认证通过前：存放的是电话号码（用户名） 认证通过后：userDetails 对象
     */
    private final Object principal;

    private String smsCode;


    public SmsAuthenticationToken(Object principal, String smsCode) {
        super(null);
        this.principal = principal;
        this.smsCode = smsCode;
        this.setAuthenticated(false);
    }

    public SmsAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        // 这里要重点强调一下，一定要调用父类的方法
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    public String getSmsCode() {
        return smsCode;
    }
}
